package com.mrd.frame.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mrd.frame.dao.TradeStatementDao;
import com.mrd.frame.domain.TradeStatement;
import com.mrd.frame.domain.message.ReconciliationMessage;
import com.mrd.frame.domain.params.ReconciliationParams;
import com.mrd.frame.remote.Validate;
import com.mrd.frame.service.TradeStatementService;
import com.mrd.weixinpay.util.BeanUtil;

@Service
public class TradeStatementServiceImpl implements TradeStatementService {
	private Logger logger = Logger.getLogger(TradeStatementServiceImpl.class);
	
	@Autowired
	private TradeStatementDao tradeStatementDao;
	@Autowired
	private Validate validate;
	@Autowired
	private HttpServletRequest request;
	
	@Override
	public int deleteByPrimaryKey(String tradingStatementsId) {
		return tradeStatementDao.deleteByPrimaryKey(tradingStatementsId);
	}

	@Override
	public int insert(TradeStatement record) {
		return tradeStatementDao.insert(record);
	}

	@Override
	public int insertSelective(TradeStatement record) {
		return tradeStatementDao.insertSelective(record);
	}

	@Override
	public TradeStatement selectByPrimaryKey(String tradingStatementsId) {
		return tradeStatementDao.selectByPrimaryKey(tradingStatementsId);
	}

	@Override
	public int updateByPrimaryKeySelective(TradeStatement record) {
		return tradeStatementDao.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(TradeStatement record) {
		return tradeStatementDao.updateByPrimaryKey(record);
	}

	@Override
	public ReconciliationMessage reconciliation(ReconciliationParams params) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String tradeNo = params.getTradeNO();
		String userKey = params.getUserKey();
		String fromDate = params.getFromDate();
		String toDate = params.getToDate();
		List<TradeStatement> tradeStatementList = null;
		ReconciliationMessage rm = new ReconciliationMessage();
		if(validate.inTheWhiteList(userKey, request)){
			try {
				if(tradeNo != null){
					tradeStatementList = tradeStatementDao.selectByTradeNo(tradeNo, userKey);
				}else {
						tradeStatementList = tradeStatementDao.selectInPeriod(userKey, sdf.parse(fromDate), sdf.parse(toDate));
				}

			} catch (ParseException e) {
				rm.setCode(-1);
				rm.setMessage("日期格式不正确，必须为yyyy-MM-dd HH:mm:ss");
				e.printStackTrace();
				logger.error("获取对账单失败", e);
			} catch (Exception e){
				rm.setCode(-1);
				rm.setMessage(e.getMessage());
				e.printStackTrace();
				logger.error("获取对账单失败", e);
			}
		}else{
			rm.setCode(-1);
			rm.setMessage("白名单验证不通过");
		}
		rm = BeanUtil.changeToReconciliationMessage(tradeStatementList);
		rm.setFromDate(fromDate);
		rm.setToDate(toDate);
		rm.setTradeNO(tradeNo);
		rm.setUserKey(userKey);
		return rm;
	}

	@Override
	public List<TradeStatement> selectSelective(TradeStatement record) {
		return tradeStatementDao.selectSelective(record);
	}
}